home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / prio / _k_heap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  4.0 KB  |  228 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _k_heap.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/impl/k_heap.h>
  16.  
  17.  
  18. #define KEY(i)   (HEAP[i]->key)
  19. #define INF(i)   (HEAP[i]->inf)
  20.  
  21.  
  22. k_heap::k_heap(int n, int k)  
  23. { if (n<=0) error_handler(1,string("k_heap constructor: illegal size = %d",n));
  24.   K = k;
  25.   HEAP = new k_heap_item[n+2];
  26.   for(int i = 0; i <= n; i++) HEAP[i] = nil;
  27.   count = 0; 
  28.   max_size = n;
  29. }
  30.  
  31. k_heap::k_heap(const k_heap& H)
  32. { K = H.K;
  33.   max_size = H.max_size;
  34.   count = H.count; 
  35.   HEAP = new k_heap_item[max_size+2];
  36.   for(int i = 1; i <= count; i++) 
  37.   { HEAP[i] = new k_heap_elem(H.HEAP[i]->key, H.HEAP[i]->inf,i);
  38.     H.copy_key(HEAP[i]->key);
  39.     H.copy_inf(HEAP[i]->inf);
  40.   }
  41. }
  42.  
  43. k_heap& k_heap::operator=(const k_heap& H)
  44. { clear();
  45.   delete HEAP;
  46.   K = H.K;
  47.   max_size = H.max_size;
  48.   count = H.count; 
  49.   HEAP = new k_heap_item[max_size+2];
  50.   for(int i = 1; i <= count; i++) 
  51.   { HEAP[i] = new k_heap_elem(H.HEAP[i]->key, H.HEAP[i]->inf,i);
  52.     copy_key(HEAP[i]->key);
  53.     copy_inf(HEAP[i]->inf);
  54.   }
  55.   return *this;
  56. }
  57.  
  58. k_heap::~k_heap()  
  59. { clear();
  60.   delete HEAP; 
  61. }
  62.  
  63. void k_heap::clear()
  64. { for(int i=1; i <= count; i++) 
  65.   { clear_key(KEY(i));
  66.     clear_inf(INF(i));
  67.     delete HEAP[i];
  68.    }
  69.   count = 0;
  70. }
  71.  
  72.  
  73. void k_heap::rise(int pos, k_heap_item it)
  74.   HEAP[0] = it;  // use "it" as stopper
  75.  
  76.   register int         pi = pos/K;        // parent index
  77.   register k_heap_item p  = HEAP[pi];     // parent node
  78.  
  79.  
  80.   if (int_type())
  81.      while(p->key > it->key)
  82.      { HEAP[pos] = p;
  83.        p->index = pos;
  84.        pos = pi;
  85.        pi /= K;
  86.        p = HEAP[pi];
  87.       }
  88.   else
  89.      while (cmp(p->key,it->key) > 0)
  90.      { HEAP[pos] = p;
  91.        p->index = pos;
  92.        pos = pi;
  93.        pi /= K;
  94.        p = HEAP[pi];
  95.       }
  96.  
  97.   HEAP[pos] = it;
  98.   it->index = pos;
  99. }
  100.  
  101.  
  102. void k_heap::sink(int pos, k_heap_item it)
  103.   register int ci = K*pos;    // child index
  104.   register k_heap_item p;     // child node
  105.  
  106.  
  107.   HEAP[count+1] = HEAP[count];   // stopper
  108.  
  109.   if (int_type())
  110.      while (ci <= count)
  111.      { 
  112.        p = HEAP[ci];
  113.  
  114.        int r = Min(count+1,ci+K);
  115.  
  116.        for (int j=ci+1;j<r;j++)
  117.            if (cmp(KEY(j),p->key)<0 ) 
  118.             { ci = j;
  119.               p = HEAP[j];
  120.              }
  121.  
  122.        if (it->key > p->key) 
  123.        { HEAP[pos] = p;
  124.          p->index = pos;
  125.          pos = ci;
  126.          ci *= K;
  127.         }
  128.        else  break;
  129.       }
  130.   else
  131.      while (ci <= count)
  132.      { 
  133.        p = HEAP[ci];
  134.  
  135.        int r = Min(count+1,ci+K);
  136.  
  137.        for (int j=ci+1;j<r;j++)
  138.            if (cmp(KEY(j),p->key)<0 ) 
  139.             { ci = j;
  140.               p = HEAP[j];
  141.              }
  142.        
  143.        if (cmp(it->key,p->key)>0) 
  144.        { HEAP[pos] = p;
  145.          p->index = pos;
  146.          pos = ci;
  147.          ci *= K;
  148.         }
  149.         else  break;
  150.       }
  151.  
  152.   HEAP[pos] =it;
  153.   it->index = pos;
  154. }
  155.  
  156.  
  157.  
  158.  
  159. void k_heap::decrease_key(k_heap_item it, GenPtr k)
  160. {
  161. //if (cmp(it->key,k)<0) error_handler(1,"k-heap:key too large in decrease_key");
  162.   clear_key(it->key);
  163.   copy_key(k);
  164.   it->key = k;
  165.   rise(it->index, it);
  166. }
  167.  
  168.  
  169.  
  170. k_heap_item k_heap::insert(GenPtr k, GenPtr i) 
  171. {
  172.   if (count == max_size) error_handler(1,"k_heap: overflow");
  173.   count++;
  174.   copy_key(k);
  175.   copy_inf(i);
  176.   k_heap_item it = new k_heap_elem(k,i,count);
  177.   rise(count,it);
  178.  
  179.   return it;
  180. }
  181.  
  182.  
  183.  
  184.  
  185. void k_heap::del_item(k_heap_item it)
  186. { k_heap_item p = HEAP[count];
  187.  
  188.   HEAP[count] = nil;
  189.  
  190.   count--;
  191.  
  192.   if (it != p)
  193.   { if (cmp(p->key,it->key) > 0)
  194.        sink(it->index, p);
  195.     else
  196.        rise(it->index, p);
  197.    }
  198.  
  199.   clear_key(it->key);
  200.   clear_inf(it->inf);
  201.  
  202.   delete it;
  203.  
  204.  
  205. }
  206.  
  207. void k_heap::change_inf(k_heap_item it, GenPtr i) 
  208. { clear_inf(it->inf);
  209.   copy_inf(i);
  210.   it->inf = i; 
  211.  }
  212.  
  213. void k_heap::print()
  214.   cout << "count = " << count << endl;
  215.   for(int i=1;i<=count;i++) 
  216.   { print_key(KEY(i));
  217.     //cout << "-";
  218.     //print_inf(INF(i));
  219.     cout << "  ";
  220.    }
  221.   newline;
  222. }
  223.  
  224.